<?php

namespace UF\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use UF\AccountBundle\Entity\Nationality;
use UF\AccountBundle\Form\NationalityType;
use UF\AccountBundle\Form\NationalityHandler;

class NationalityController extends Controller
{
    
    public function listNationalitiesAction()
    {   	
    	$em = $this->getDoctrine()->getRepository('UFAccountBundle:Nationality');
    	$nats = $em->findAll();   	
    	$params = array("nats" => $nats);   	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Nationalities:nationalitiesList.html.twig', $params);
    }
    
    public function addNationalityAction()
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();

    	$nat = new Nationality();
    	$natForm = $this->createForm(new NationalityType(), $nat);  		
    	$natHandler = new NationalityHandler($natForm, $request, $em);
    	
    	try{
	    	if($natHandler->process()){
	    		return $this->redirect($this->generateUrl("UFAccountBundle_listNationalities"));
	    	}
    	}
    	catch(\Exception $e){throw $this->createNotFoundException("La nationalité n'a pas pu être ajoutée");}
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Nationalities:addNationality.html.twig', array("natForm" => $natForm->createView()));
    }
    
    public function modifyNationalityAction($natId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Nationality');

    	$nat = $repo->find($natId);
    	$natForm = $this->createForm(new NationalityType(), $nat);
    	$natHandler = new NationalityHandler($natForm, $request, $em);

    	try{
    		if($natHandler->process()){
    			return $this->redirect($this->generateUrl("UFAccountBundle_listNationalities"));
    		}
    	}
    	catch(\Exception $e){
    		throw $this->createNotFoundException("La nationalité n'a pas pu être modifiée");
    	}
    	
    	$this->getRequest()->getSession()->set("lastRoute", $this->getRequest()->get('_route'));
    	return $this->render('UFAccountBundle:Nationalities:modifyNationality.html.twig', array("natForm" => $natForm->createView(), "natId" => $natId));
    	
    }
       
    public function deleteNationalityAction($natId)
    {
    	$request = $this->getRequest();
    	$em = $this->getDoctrine()->getEntityManager();
    	$repo = $this->getDoctrine()->getRepository('UFAccountBundle:Nationality');
    	 
    	try
    	{
    		$nat = $repo->find($natId);
    		$em->remove($nat);
    		$em->flush();
    	}
    	catch (\Exception $e)
    	{
    		throw $this->createNotFoundException("La nationalité n'a pas pu être supprimée");
    	}
    	 
    	return $this->redirect($this->generateUrl("UFAccountBundle_listNationalities"));
    }
    
}
